package com.fub.view;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.fub.R;
import com.fub.guidance.Graph;
import com.fub.guidance.Vertex;
import com.fub.util.PreferenceDAO;

public class DetailActivity extends Activity {

	private static final int TEXT_SIZE = 12;
	private static final int FORMATED_TEXT_WIDTH = 120;
	private static final String IMAGE_KEY = "imageurl";
	private static final String PHONE_KEY = "Telefon";
	private static final String IMAGE_LOCATION_SERVER = "roomimages/";
	private static final int PADDING = 10;
	private static final String NOIMAGE = "no_image.jpg";
	private String server;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.roomdetail);
		this.server = new PreferenceDAO(this).getServerName()
				+ IMAGE_LOCATION_SERVER;
		setImage(NOIMAGE);
		fillTable();
	}

	private void setImage(String imageName) {
		Drawable image = ImageOperations(this, this.server + imageName,
				imageName);
		ImageView imgView = new ImageView(this);
		imgView = (ImageView) findViewById(R.id.roomDetailImageView);
		imgView.setImageDrawable(image);
	}

	private Drawable ImageOperations(Context ctx, String url,
			String saveFilename) {
		try {
			InputStream is = (InputStream) this.fetch(url);
			Drawable d = Drawable.createFromStream(is, "src");
			return d;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	private Object fetch(String address) throws MalformedURLException,
			IOException {
		URL url = new URL(address);
		Object content = url.getContent();
		return content;
	}

	private void fillTable() {
		TableLayout tl = (TableLayout) findViewById(R.id.detailtableLayout);
		TextView tv = (TextView) findViewById(R.id.roomLabelTextView);
		Vertex selectedNode = Graph.getInstance().getNodeById(
				new PreferenceDAO(this).getSelectedNodeId());
		tv.setText(selectedNode.getLabel());

		for (String key : selectedNode.getProperies().keySet()) {
			if (key.equals(IMAGE_KEY)) {
				setImage(selectedNode.getProperty(key));
				continue;
			}
			TableRow row = new TableRow(this);

			TextView textViewKey = createFormatedTextView(key, true);
			row.addView(textViewKey);
			// set telephone Link

			TextView textViewValue = createFormatedTextView(
					selectedNode.getProperty(key), false);

			if (key.equals(PHONE_KEY)) {
				final String number = selectedNode.getProperty(key);
				textViewValue.setLinksClickable(true);
				textViewValue.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(Intent.ACTION_DIAL,
						Uri.parse("tel:"+ number));
						startActivity(intent);
					}
				});

			}
			row.addView(textViewValue);
			tl.addView(row, new TableLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

		}
	}

	private TextView createFormatedTextView(String text, boolean bold) {
		TextView textView = new TextView(this);
		textView.setText(text);
		textView.setWidth(FORMATED_TEXT_WIDTH);
		textView.setSingleLine(false);
		textView.setTextSize(TEXT_SIZE);
		textView.setPadding(PADDING, PADDING, PADDING, PADDING);
		if (bold) {
			textView.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
		}
		textView.setTextColor(Color.WHITE);
		return textView;
	}
}